package com.example.demo.service.impl;

import com.example.demo.entity.Instorage;
import com.example.demo.entity.Storage;
import com.example.demo.mapper.InstorageMapper;
import com.example.demo.service.InstorageService;
import com.example.demo.service.StorageService;
import com.example.demo.service.helper.StorageInventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class InstorageServiceImpl implements InstorageService {

    @Autowired
    private InstorageMapper instorageMapper;

    @Autowired
    private StorageService storageService;

    @Override
    public double getRealWeight(Integer iid) {
        return instorageMapper.findRealWeight(iid);
    }

    @Override
    public int createInstorage(Instorage instorage) {
        // 检查仓库容量
        if (!storageService.checkStorageCapacity(
                instorage.getIsbelong(),
                instorage.getIweight(),
                instorage.getIquantity())) {
            throw new RuntimeException("仓库容量不足");
        }

        // 设置真实吨数（初始等于入库吨数）
        instorage.setIrweight(instorage.getIweight());

        int result = instorageMapper.createInstorage(instorage);

        if (result > 0) {
            // 更新仓库库存（实际项目中应使用触发器）
            Storage storage = storageService.findStorageById(instorage.getIsbelong());
            storage.setSweight(storage.getSweight() + instorage.getIweight());
            storage.setSquantity(storage.getSquantity() + instorage.getIquantity());
            storageService.updateStorage(storage);
        }

        return result;
    }

    @Override
    public int deleteInstorage(Integer iid) {
        Instorage instorage = instorageMapper.findInstorageById(iid);
        if (instorage == null) {
            throw new RuntimeException("入库记录不存在");
        }

        int result = instorageMapper.deleteInstorage(iid);

        if (result > 0) {
            // 更新仓库库存
            Storage storage = storageService.findStorageById(instorage.getIsbelong());
            storage.setSweight(storage.getSweight() - instorage.getIweight());
            storage.setSquantity(storage.getSquantity() - instorage.getIquantity());
            storageService.updateStorage(storage);
        }

        return result;
    }

    @Override
    public Instorage findInstorageById(Integer iid) {
        return instorageMapper.findInstorageById(iid);
    }

    @Override
    public List<Instorage> findAllInstorages() {
        return instorageMapper.findAllInstorages();
    }

    @Override
    public List<Instorage> findInstoragesByStorageId(Integer sid) {
        return instorageMapper.findInstoragesByStorageId(sid);
    }

    @Override
    public List<Instorage> findInstoragesByCarNum(String carNum) {
        return instorageMapper.findAllInstorages().stream()
                .filter(i -> carNum.equals(i.getIcarnum()))
                .collect(Collectors.toList());
    }

    @Override
    public StorageInventory getStorageInventory(Integer sid) {
        List<Instorage> instorages = instorageMapper.findInstoragesByStorageId(sid);
        double totalWeight = instorages.stream().mapToDouble(Instorage::getIrweight).sum();
        int totalQuantity = instorages.stream().mapToInt(Instorage::getIquantity).sum();

        return new StorageInventory(totalWeight, totalQuantity);
    }

    @Override
    public void updateRealWeight(Integer iid, double irweight) {
        instorageMapper.updateRealWeight(iid, irweight);
    }
}
